package views;

import info.clearthought.layout.TableLayout;

import java.awt.Font;
import java.util.Hashtable;

import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JSlider;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import models.AttractorGen;

import org.cheffo.jeplite.JEP;

import controllers.MainController;

public class ControlPanel extends JPanel
{
	/**
	 * Generated serial ID
	 */
	private static final long						serialVersionUID	= -6393075287162512375L;
	
	public JEP										eqParser			= null;
	
	private JScrollPane								eqScrollX			= null;
	private JScrollPane								eqScrollY			= null;
	private JScrollPane								eqScrollZ			= null;
	
	private JPanel									panePtsNumberField	= null;		
	private JPanel 									sliderPane			= null;
	private JPanel 									staticDynamicPane	= null;
	
	private VarPanel								varPanel			= null;
	
	private JTextArea								eqAreaX				= null;
	private JTextArea								eqAreaY				= null;
	private JTextArea								eqAreaZ				= null;
	
	private JSlider									slider				= null;

	private JTextField 								ptsNumberField		= null;
	
	private JLabel 									sliderLabel			= null;
	
	private JRadioButton 							dynamicRadio		= null;
	private JRadioButton 							staticRadio			= null;
	
	private int										eqAreaSize			= 75;
	private int										sliderSize			= 100;
	private int										separator			= 10;
	private int										HGAP				= 10;
	

	private MainController 							mainController 		= null;

	private JButton ptsNumberButton;


	public ControlPanel(MainController mainController)
	{
		this.mainController = mainController;
		
		setSize(192, Canvas.HEIGHT);
		
		setBorder(BorderFactory.createTitledBorder("Control Panel"));
		
		double size[][] =
		{
						{ separator, TableLayout.FILL, separator },
						{
							separator,
							eqAreaSize,
							HGAP,
							eqAreaSize,
							HGAP,
							eqAreaSize,
							HGAP,
							separator,
							sliderSize,
							HGAP,
							60,
							HGAP,
							60,
							HGAP,
							separator,
							TableLayout.FILL
						}
		};
		
		setLayout(new TableLayout(size));
		
		add(getEqScrollX(), "1, 1");
		add(getEqScrollY(), "1, 3");
		add(getEqScrollZ(), "1, 5");
		add(new JSeparator(), "1, 7");
		add(getSliderPane(), "1, 8");
		add(getPanePointsNumberSetter(), "1, 10");
		add(getStaticDynamicPane(), "1, 12");
		add(new JSeparator(), "1, 14");
		add(getVarPanel(), "1, 15");
	}
	
	private JScrollPane getEqScrollX()
	{
		if (eqScrollX == null)
		{
			eqScrollX = new JScrollPane(getEqAreaX());
			eqScrollX.setBorder(BorderFactory.createTitledBorder("Equation for X"));
		}
		
		return eqScrollX;
	}
	
	private JScrollPane getEqScrollY()
	{
		if (eqScrollY == null)
		{
			eqScrollY = new JScrollPane(getEqAreaY());
			eqScrollY.setBorder(BorderFactory.createTitledBorder("Equation for Y"));
		}
		
		return eqScrollY;
	}
	
	private JScrollPane getEqScrollZ()
	{
		if (eqScrollZ == null)
		{
			eqScrollZ = new JScrollPane(getEqAreaZ());
			eqScrollZ.setBorder(BorderFactory.createTitledBorder("Equation for Z"));
		}
		
		return eqScrollZ;
	}
	
	private JTextArea getEqAreaX()
	{
		if (eqAreaX == null)
		{
			eqAreaX = new JTextArea();
			// set the default text for presentation purposes
			eqAreaX.setText("sinf(A * getY(currentPos, offset)) - getZ(currentPos, offset) * cosf(B * getX(currentPos, offset))");
//			eqAreaX.setLineWrap(true);
			eqAreaX.setColumns(10);
			eqAreaX.setRows(4);
			eqAreaX.setFont(new Font("Arial", Font.PLAIN, 12));
		}
		
		return eqAreaX;
	}

	private JTextArea getEqAreaY()
	{
		if (eqAreaY == null)
		{
			eqAreaY = new JTextArea();
			// set the default text for presentation purposes
			eqAreaY.setText("getZ(currentPos, offset) * sinf(C * getX(currentPos, offset)) + cosf(D * getY(currentPos, offset))");
			eqAreaY.setColumns(10);
			eqAreaY.setRows(4);
			eqAreaY.setFont(new Font("Arial", Font.PLAIN, 12));
		}
		
		return eqAreaY;
	}

	private JTextArea getEqAreaZ()
	{
		if (eqAreaZ == null)
		{
			eqAreaZ = new JTextArea();
			// set the default text for presentation purposes
			eqAreaZ.setText("sinf(getX(currentPos, offset))");
			eqAreaZ.setColumns(10);
			eqAreaZ.setRows(4);
			eqAreaZ.setFont(new Font("Arial", Font.PLAIN, 12));
		}
		
		return eqAreaZ;
	}
	
	public String getEqX()
	{
		if (eqAreaX != null)
		{
			return eqAreaX.getText();
		}
		
		return new String();
	}
	
	public String getEqY()
	{
		if (eqAreaY != null)
		{
			return eqAreaY.getText();
		}
		
		return new String();
	}
	
	public String getEqZ()
	{
		if (eqAreaZ != null)
		{
			return eqAreaZ.getText();
		}
		
		return new String();
	}
	
	private JPanel getSliderPane()
	{
		if (sliderPane == null)
		{
			sliderPane = new JPanel();
			sliderPane.setBorder(BorderFactory.createTitledBorder("Decrement/Increment Step"));
			
			double size[][] =
			{
							{ TableLayout.FILL }, 
                            { TableLayout.FILL, 20, 5 } 
			};
			
			sliderPane.setLayout(new TableLayout(size));
			
			sliderPane.add(getSlider(), "0, 0");
			sliderPane.add(getSliderLabel(), "0, 1");
		}
		
		return sliderPane;
	}
	
	private JSlider getSlider()
	{
		if (slider == null)
		{
			slider = new JSlider(JSlider.HORIZONTAL, -500, 500, (int)(AttractorGen.STEP * 100000));
			Hashtable<Integer, JLabel> table = new Hashtable<Integer, JLabel>();
		    
			table.put(-500, new JLabel("-0.005"));
		    table.put(-300, new JLabel("-0.003"));
		    table.put(-100, new JLabel("-0.001"));
		    table.put(0, new JLabel("0"));
		    table.put(100, new JLabel("0.001"));
		    table.put(300, new JLabel("0.003"));
		    table.put(500, new JLabel("0.005"));
		    
		    slider.setLabelTable(table);
		    slider.setPaintLabels(true);
		    
		    slider.addChangeListener(mainController);
		}
		
		return slider;
	}
	
	public JSlider getSliderItem()
	{
		return slider;
	}
	
	public void setSliderItemValue(float value)
	{
		int tempVal = (int)(value * 10000);
		slider.setValue(tempVal);
	}
	
	public JLabel getSliderLabel()
	{
		if (sliderLabel == null)
		{
			sliderLabel = new JLabel(String.valueOf(AttractorGen.STEP), JLabel.CENTER);
		}
		
		return sliderLabel;
	}
	
	private JPanel getPanePointsNumberSetter()
	{
		if (panePtsNumberField == null)
		{
			panePtsNumberField = new JPanel();
			panePtsNumberField.setBorder(BorderFactory.createTitledBorder("Number of points (max "
					+ AttractorGen.MAX_NR_OF_PTS + ")"));
			
			double size[][] =
			{
							{ 200, TableLayout.FILL, 25 }, 
                            { TableLayout.FILL, 5 } 
			};
			
			panePtsNumberField.setLayout(new TableLayout(size));
			panePtsNumberField.add(getPointsNumberSetter(), "0, 0");
			panePtsNumberField.add(getPointsNumberSetterButton(), "1, 0");
		}
		
		return panePtsNumberField;
	}
	
	public JTextField getPointsNumberSetter()
	{
		if (ptsNumberField == null)
		{
			ptsNumberField = new JTextField(String.valueOf(AttractorGen.NR_OF_PTS));
//			ptsNumberField.setHorizontalAlignment(JTextField.CENTER);
		}

		return ptsNumberField;
	}
	
	public JButton getPointsNumberSetterButton()
	{
		if (ptsNumberButton == null)
		{
			ptsNumberButton = new JButton("Set");
			ptsNumberButton.setActionCommand("SET_PTS_NR");
			ptsNumberButton.addActionListener(mainController);
		}
		return ptsNumberButton;	
	}
	
	public String getPointsNumberFromTF()
	{
		return ptsNumberField.getText();
	}
	
	private JPanel getStaticDynamicPane()
	{
		if (staticDynamicPane == null)
		{
			staticDynamicPane = new JPanel();
			
			staticDynamicPane.setBorder(BorderFactory.createTitledBorder("Static or Dynamic Attractor"));
			
			ButtonGroup group = new ButtonGroup();
			group.add(getStaticButton());
			group.add(getDynamicButton());
			
			staticDynamicPane.add(getDynamicButton());
			staticDynamicPane.add(getStaticButton());
		}

		return staticDynamicPane;
	}

	private JRadioButton getDynamicButton() 
	{
		if (dynamicRadio == null)
		{
			dynamicRadio = new JRadioButton("Dynamic");
			dynamicRadio.setSelected(true);
			dynamicRadio.setActionCommand("DYNAMIC");
			dynamicRadio.addActionListener(mainController);
		}

		return dynamicRadio;
	}

	private JRadioButton getStaticButton() 
	{
		if (staticRadio == null)
		{
			staticRadio = new JRadioButton("Static");
			staticRadio.setActionCommand("STATIC");
			staticRadio.addActionListener(mainController);
		}

		return staticRadio;
	}
	
	public VarPanel getVarPanel()
	{
		if (varPanel == null)
		{
			varPanel = new VarPanel(mainController);
		}
		
		return varPanel;
	}
}
